package com.snailxr.base.task;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.snailxr.base.support.util.Util;
import com.snailxr.base.task.service.APIService;
import com.snailxr.base.task.service.DataTransferService;

/**
 * 任务类和方法
 * 根据接口获取数据，导入到数据库中
 * @author luoshijian
 *
 */
@Component
public class DataTransfer {

	public final Logger log = Logger.getLogger(this.getClass());
	@Autowired
	private DataTransferService dataTransferService;
	@Autowired
	private APIService apiService;
	@Value("#{conf[xzspDataTransferCollection]}")
	private String xzspDataTransferCollection;//行政审批数据传输表
	@Value("#{conf[bsznDataTransferCollection]}")
	private String bsznDataTransferCollection;//办事指南数据传输表
	
	/**
	 * 获取行政审批数据
	 */
	public void transXzspDatas() {
		log.info(" run................传输行政审批数据......................" + (new Date()));
//		APIObj apiObj = apiService.getByApiId(1);//FIXME
//		log.debug(apiObj);
//		String result = httpUtil.sendGet(apiObj);
		
		String result="[]";
		log.info("数据文件路径:"+this.getClass().getResource("/").toString().substring(6)+"xzsp.txt");
		result = Util.readFile2String(this.getClass().getResource("/").toString().substring(6)+"xzsp.txt");
		
		try {
			result = URLDecoder.decode(result,"UTF-8");//解码
			JSONArray jsonArr = JSONArray.parseArray(result);
			log.info("传输行政审批数据总计："+jsonArr.size());
			dataTransferService.deleteAll(xzspDataTransferCollection);//清空数据库
			for(int i=0;i<jsonArr.size();i++){
				dataTransferService.insertJson(jsonArr.getJSONObject(i), xzspDataTransferCollection);
			}
			log.info("传输行政审批数据完成！");
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
	}
	/**
	 * 获取办事指南数据
	 */
	public void transBsznDatas() {
		log.info(" run................传输办事指南数据......................" + (new Date()));
//		APIObj apiObj = apiService.getByApiId(1);//FIXME
//		log.debug(apiObj);
//		String result = httpUtil.sendGet(apiObj);
		
		String result="[]";
		log.info("数据文件路径:"+this.getClass().getResource("/").toString().substring(6)+"bszn.txt");
		result = Util.readFile2String(this.getClass().getResource("/").toString().substring(6)+"bszn.txt");
		
		try {
			result = URLDecoder.decode(result,"UTF-8");//解码
			JSONArray jsonArr = JSONArray.parseArray(result);
			log.info("传输办事指南数据总计："+jsonArr.size());
			dataTransferService.deleteAll(bsznDataTransferCollection);//清空数据库
			for(int i=0;i<jsonArr.size();i++){
				dataTransferService.insertJson(jsonArr.getJSONObject(i), bsznDataTransferCollection);
			}
			log.info("传输办事指南数据完成！");
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
	}
	
	public String getXzspDataTransferCollection() {
		return xzspDataTransferCollection;
	}
	public void setXzspDataTransferCollection(String xzspDataTransferCollection) {
		this.xzspDataTransferCollection = xzspDataTransferCollection;
	}
	public String getBsznDataTransferCollection() {
		return bsznDataTransferCollection;
	}
	public void setBsznDataTransferCollection(String bsznDataTransferCollection) {
		this.bsznDataTransferCollection = bsznDataTransferCollection;
	}
}
